﻿Public Class frmLED8Segment
    Private Enum GaugeAnimation As Integer
        Increase = 0
        Decrease = 1
    End Enum
    Private _AnimateGaugeDirection As GaugeAnimation = GaugeAnimation.Increase




    Private Sub frmLED8Segment_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LeD8Segment1.Value = 0
        _AnimateGaugeDirection = GaugeAnimation.Increase
        Timer1.Enabled = False
        Timer1.Interval = 50
        btnStartGauge.Text = "Start Gauge"

        'Set up data sources from FillStyle and FillGradientStyle combo boxes
        cboFillGradientStyle.DataSource = System.Enum.GetValues(GetType(System.Drawing.Drawing2D.LinearGradientMode))
        cboFillStyle.DataSource = System.Enum.GetValues(GetType(PolyMonControls.LED8Segment.BackgroundType.FillStyles))
        

        'Bind Form objects to Gradient Panel Properties
        cboFillGradientStyle.DataBindings.Add("SelectedItem", LeD8Segment1.Background, "FillGradientStyle")
        cboFillStyle.DataBindings.Add("SelectedItem", LeD8Segment1.Background, "FillStyle")
        pbBackColor1.DataBindings.Add("BackColor", LeD8Segment1.Background, "BackColor1")
        pbBackColor2.DataBindings.Add("BackColor", LeD8Segment1.Background, "BackColor2")

       
        nudPaddingTop.DataBindings.Add("Value", LeD8Segment1.Padding, "Top")
        nudPaddingBottom.DataBindings.Add("Value", LeD8Segment1.Padding, "Bottom")
        nudPaddingLeft.DataBindings.Add("Value", LeD8Segment1.Padding, "Left")
        nudPaddingRight.DataBindings.Add("Value", LeD8Segment1.Padding, "Right")

        nudSegmentThickness.DataBindings.Add("Value", LeD8Segment1, "SegmentThickness")
        pbLitColor.DataBindings.Add("BackColor", LeD8Segment1, "LitColor")
        pbUnlitColor.DataBindings.Add("BackColor", LeD8Segment1, "UnlitColor")
        chkTruncateZeroes.DataBindings.Add("Checked", LeD8Segment1, "TruncateTrailingZeroes")

        nudNumDigits.DataBindings.Add("Value", LeD8Segment1, "NumDigits")
        nudPrecision.DataBindings.Add("Value", LeD8Segment1, "Precision")

        'Add handlers for color selections
        AddHandler pbBackColor1.Click, AddressOf ChooseColor
        AddHandler pbBackColor2.Click, AddressOf ChooseColor
        AddHandler pbLitColor.Click, AddressOf ChooseColor
        AddHandler pbUnlitColor.Click, AddressOf ChooseColor


        'Add Handler for Form object property changes
        AddHandler cboFillStyle.SelectedIndexChanged, AddressOf ControlValueChanged
        AddHandler cboFillGradientStyle.SelectedIndexChanged, AddressOf ControlValueChanged
        AddHandler pbBackColor1.BackColorChanged, AddressOf ControlValueChanged
        AddHandler pbBackColor2.BackColorChanged, AddressOf ControlValueChanged


        AddHandler nudPaddingTop.ValueChanged, AddressOf UpdatePadding
        AddHandler nudPaddingBottom.ValueChanged, AddressOf UpdatePadding
        AddHandler nudPaddingLeft.ValueChanged, AddressOf UpdatePadding
        AddHandler nudPaddingRight.ValueChanged, AddressOf UpdatePadding

        AddHandler nudSegmentThickness.ValueChanged, AddressOf ControlValueChanged
        AddHandler pbLitColor.BackColorChanged, AddressOf ControlValueChanged
        AddHandler pbUnlitColor.BackColorChanged, AddressOf ControlValueChanged
        AddHandler chkTruncateZeroes.CheckedChanged, AddressOf ControlValueChanged

        AddHandler nudNumDigits.ValueChanged, AddressOf ControlValueChanged
        AddHandler nudPrecision.ValueChanged, AddressOf ControlValueChanged
    End Sub

    Private Sub btnStartGauge_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartGauge.Click
        If Timer1.Enabled Then
            Timer1.Enabled = False
            btnStartGauge.Text = "Start Gauge"
        Else
            Timer1.Enabled = True
            btnStartGauge.Text = "Stop Gauge"
        End If

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim NumDigits As Integer = LeD8Segment1.NumDigits
        Dim Precision As Integer = LeD8Segment1.Precision

        'If Precision > -NumDigits Then Precision = NumDigits - 1
        Dim MaxValue As Single = CSng(New String(CChar("9"), NumDigits - Precision))

        If LeD8Segment1.Value >= MaxValue Then
            _AnimateGaugeDirection = GaugeAnimation.Decrease
            LeD8Segment1.Value = MaxValue
        ElseIf LeD8Segment1.Value <= -MaxValue Then
            _AnimateGaugeDirection = GaugeAnimation.Increase
            LeD8Segment1.Value = -MaxValue
        End If

        Dim StepSize As Double = 1
        If Precision <= 0 Then
            StepSize = 1
        Else
            StepSize = 1.0! / (10 ^ CDbl(Precision))
        End If

        Select Case _AnimateGaugeDirection
            Case GaugeAnimation.Increase
                LeD8Segment1.Value += StepSize
            Case GaugeAnimation.Decrease
                LeD8Segment1.Value -= StepSize
        End Select
    End Sub

    Private Sub ChooseColor(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim pb As PictureBox = CType(sender, PictureBox)
        With Me.ColorDialog1
            .Color = pb.BackColor
            Dim res As DialogResult = .ShowDialog()

            If res = Windows.Forms.DialogResult.OK Then
                pb.BackColor = .Color
            End If
        End With
    End Sub

    Private Sub ControlValueChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim Ctl As Control = CType(sender, Control)
        If Ctl.DataBindings.Count > 0 Then
            Ctl.DataBindings(0).WriteValue()
        End If
    End Sub

    Private Sub UpdatePadding()
        LeD8Segment1.Padding = New Padding(nudPaddingLeft.Value, nudPaddingTop.Value, nudPaddingRight.Value, nudPaddingBottom.Value)
    End Sub
End Class